using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Ddb_Demo.SQL
{
    public class DetachSQL
    {
        public DetachSQL()
        {
           
        
        }
        public string s1 = @"\s*Select|select|SELECT\s+";
        public string u1 = @"\s*Update|UPDATE|update\s+";
        public string d1 = @"\s*Delete|DELETE|delete\s+";
        public string s2 = @"\s+From|from|FROM\s+";
        public string s3 = @"\s+Where|where|WHERE\s+";
        public string s4 = @"\s*,\s*";
        public string s5 = @"\s+and|AND|And\s+";


        //public SqlSentence check(string sql)
        //{

            
        //    SqlSentence ss = new SqlSentence();

        //    if (ismatch(sql, s1))
        //    {
        //        ss = detach("select", sql, ss);
        //        return ss;

        //    }
        //    else if (ismatch(sql, u1))
        //    {
        //        ss = detach("update", sql, ss);
        //        return ss;
        //    }
        //    else if (ismatch(sql, d1))
        //    {
        //        ss = detach("delete", sql, ss);
        //        return ss;
        //    }
        //    else
        //    {
        //        error("undefined action");
        //        return null;
        //    }


        //}
        public bool ismatch(string sql, string pat)
        {
            Regex r = new Regex(pat);
            Match m = r.Match(sql);
            if (m.Success)
                return true;
            else return false;
        }
        public void error(string str)
        {
            System.Console.WriteLine("there is a grammer error about " + str);
        }
        public void detachSql(string sql, SqlSentence ss)
        {
                
                if (findTimes(sql, s2)==1 && findTimes(sql, s3)==1)
                {
                
                    
                    string aims = getPart(s1, s2, sql);
                    Regex r = new Regex(s4);
                    string[] Aims = r.Split(aims);
                    ss.aims[0] = Aims.Length.ToString();
                    for (int i = 0; i < Aims.Length; i++)
                        ss.aims.SetValue(Aims[i],i+1);

                    string fields = getPart(s2, s3, sql);
                    string[] Fields = r.Split(fields);
                    for(int j = 0; j < Fields.Length; j++)
                        ss.fields[j] = Fields[j];

                    Regex r2 = new Regex(s3);
                    string[] tempcon = r2.Split(sql);
                    Regex r3 = new Regex(s5);
                    string[] Conditions = r3.Split(tempcon[1]);
                    for (int k = 0; k < Conditions.Length; k++)
                        ss.condi[k] = Conditions[k];



                        
                }
                else
                {
                    error("error of 'from' or 'where'");
                    
                }
            
         
            
        }
        public string getPart(string s1, string s2, string s)
        {
            Regex r1 = new Regex(s1, RegexOptions.IgnoreCase);
            Regex r2 = new Regex(s2, RegexOptions.IgnoreCase);
            try
            {
                string[] str1 = r1.Split(s);
                string[] str2 = r2.Split(str1[1]);

                return str2[0];
            }
            catch (Exception e)
            {
                return " ";
            }
        }
        
        public int findTimes(string sentence, string word)
        {
            
            Regex r = new Regex(word,RegexOptions.IgnoreCase);
            Match m = r.Match(sentence);

            int times = 0;
            while (m.Success)
            {
                times = times + 1;
                m = m.NextMatch();
            }
            return times;
        
        
        }


    }
}
